#include "RenzoMustHave.h"
#include "RenzoPrecompiled.h"
#include "RenzoResourceManager.h"
#include "RenzoSceneManager.h"
#include "RenzoSceneIO.h"
#include "RenzoErrorReport.h"

namespace Renzo
{
SceneManager::SceneManager(void)
{
}

SceneManager::~SceneManager(void)
{
}

void SceneManager::registerFormat(SceneIO* io) {
	String ext = io->getFormatExtension();
	SceneIOList::iterator i = sceneios.find(ext);
	if (i != sceneios.end()) { // loader exists
		rzWarning(ERROR_ITEM_DUPLICATE_UNKNOWN);
	} else {
		sceneios[ext] = io;
	}
}

void SceneManager::unregisterFormat(const String& ext) {
	SceneIOList::iterator i = sceneios.find(ext);
	if (i != sceneios.end()) { // loader exists
		delete i->second; // delete SceneIO object
		sceneios.erase(i);
	} else {
		rzInfo(ERROR_ITEM_NOT_FOUND);
	}
}

void SceneManager::unregisterFormat(SceneIO* io) {
	String ext = io->getFormatExtension();
	this->unregisterFormat(ext);
}

SceneNode* SceneManager::load(const String& file) {
	// get file extension
	size_t pos = file.find_last_of('.');
	String ext = file.substr(pos + 1);

	// check if loader exists
	SceneIOList::iterator i = sceneios.find(ext);
	if (i != sceneios.end()) { // loader exists
		SceneIO* io = i->second;
		return io->load(file);
	} else {
		rzError(ERROR_FILE_UNKNOWN_FORMAT, ext);
	}
}

void SceneManager::save(const String& file, SceneNode* root) {
	throw ERROR_FUNCTION_NOT_IMPLEMENTED;
}

const String SceneManager::genObjectName() {
	String name;
	name.append("Object_");
	name.append(itoa(Renzoderer::instance()->getResourceManager()->genResourceID(DummyType), NULL, 10));
	return name;
}

}